﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class EquipmentProfileBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEquipmentProfileDA equipmentProfileDA = DataAccess.CreateEquipmentProfileDA();
        private PubParasInfo pubParasInfo;

        public EquipmentProfileBLL()
        {
            this.pubParasInfo = null;
        }

        public EquipmentProfileBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public EquipmentProfileBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string whLoginID = this.dataBaseID[0];
            string temp = string.Empty;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                List<EquipmentProfileInfo> equipmentProfileInfoList = new List<EquipmentProfileInfo>();
                EquipmentProfileInfo equipmentProfileInfo = null;
                foreach (string id in IDS)
                {
                    equipmentProfileInfo = equipmentProfileDA.GetEquipmentProfileByID(dataBase, tran, id, this.dataBaseID[0]);
                    equipmentProfileInfoList.Add(equipmentProfileInfo);
                }
                if (equipmentProfileDA.DeleteEquipmentProfile(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<EquipmentProfileInfo>(dataBase, tran, equipmentProfileInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "EQUIPMENT_POR_FILE", this.dataBaseID[i]);
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
                temp = temp.Substring(0, temp.Length - 1);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return equipmentProfileDA.GetAllEquipmentProfile(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return equipmentProfileDA.GetEquipmentProfileByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            EquipmentProfileQueryEntity equipmentProfileQueryEntity = obj as EquipmentProfileQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(equipmentProfileQueryEntity.EquipmentProfileID))
            {
                sqlWhere.Add(" EQUIPMENT_PROFILE_ID " + equipmentProfileQueryEntity.EquipmentProfileID);
            }
            if (!string.IsNullOrEmpty(equipmentProfileQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + equipmentProfileQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(equipmentProfileQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + equipmentProfileQueryEntity.IsActive);
            }
            return equipmentProfileDA.GetEquipmentProfileByQueryList(sqlWhere, equipmentProfileQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            EquipmentProfileInfo equipmentProfileDAInfo = obj as EquipmentProfileInfo;
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                equipmentProfileDAInfo.CreatedDate = DateTime.Now;
                equipmentProfileDAInfo.UpdatedDate = DateTime.Now;
                equipmentProfileDAInfo.UpdatedBy = equipmentProfileDAInfo.CreatedBy;
                equipmentProfileDA.InsertEquipmentProfile(dataBase, tran, equipmentProfileDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<EquipmentProfileInfo>(dataBase, tran, null, equipmentProfileDAInfo, AuditAction.Insert, this.pubParasInfo.UserID, "EQUIPMENT_POR_FILE", this.dataBaseID[0]);
                tran.Commit();
                temp = equipmentProfileDAInfo.EquipmentProfileID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string Update(object obj)
        {
            EquipmentProfileInfo equipmentProfileDAInfo = obj as EquipmentProfileInfo;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                EquipmentProfileInfo oldEquipmentProfileInfo = equipmentProfileDA.GetEquipmentProfileByID(dataBase, tran, equipmentProfileDAInfo.EquipmentProfileID, this.dataBaseID[0]);
                equipmentProfileDA.UpdateEquipmentProfile(dataBase, tran, equipmentProfileDAInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<EquipmentProfileInfo>(oldEquipmentProfileInfo, equipmentProfileDAInfo, AuditAction.Update, this.pubParasInfo.UserID, "EQUIPMENT_POR_FILE", this.dataBaseID[0]);
                tran.Commit();
                return equipmentProfileDAInfo.EquipmentProfileID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

